package cn.edu.lit.jsjxy.CourseExample.chapter3;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import cn.edu.lit.jsjxy.CourseExample.R;

public class FirstActivity extends Activity implements View.OnClickListener
{

    Button btnNext, btnThird, btnImplicit;
    EditText etUsername;
    TextView tvUsername;
    String username;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);

        btnNext = findViewById(R.id.btnNext);
        btnThird = findViewById(R.id.btnThird);
        btnImplicit = findViewById(R.id.btnImplicit);

        btnNext.setOnClickListener(this);
        btnThird.setOnClickListener(this);
        btnImplicit.setOnClickListener(this);

        etUsername = findViewById(R.id.etUsername);
        tvUsername = findViewById(R.id.tvUsername);
    }

    @Override
    public void onClick(View view)
        {
        switch (view.getId()) {
            case R.id.btnNext:

                username = etUsername.getText().toString();

                Intent intent = new Intent(this, SecondActivity.class);

                //                intent.putExtra("username", username);

                Bundle bundle = new Bundle();

                bundle.putString("username", username);

                intent.putExtras(bundle);

                startActivity(intent);
                break;

            case R.id.btnThird:

                username = etUsername.getText().toString();

                Intent intent1 = new Intent(this, ThirdActivity.class);
                intent1.putExtra("username", username);

                startActivityForResult(intent1, 1);
                break;

            case R.id.btnImplicit:
                Intent intent2 = new Intent(this, ImplicitTestActivity.class);
                startActivity(intent2);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);

        switch (resultCode) {
            case RESULT_OK:
                username = data.getStringExtra("username");
                tvUsername.setText(username);

                Toast.makeText(this, username, Toast.LENGTH_LONG).show();

                break;
            case RESULT_CANCELED:
                break;
        }
    }


}

